package com.magnificent.panda.model.generator.flow;

import java.util.ArrayList;
import java.util.Random;

/**
 * User: Babanin
 * Date: 21.09.2009
 * Time: 22:13:45
 */
public class VibrationAndObservationOmissionFlow extends AbstractFlow {
    private Random rand = new Random(/*new Date().getTime()*/);

    private int len;//длинна массива - количество отсчетов потока
    private double intensity;//интенсивность
    private int modelTime;
    private ArrayList<Double> exitX;
    private double samplingInterval;
    private double[] innerVector;

    public VibrationAndObservationOmissionFlow(int intensity1, int intensity2, int modellingTime) {
        super(intensity1, intensity2, modellingTime);
        
        this.len = (int) intensity1 * modellingTime ;
        this.intensity = modellingTime / intensity1;           
        this.modelTime = modellingTime;
        this.samplingInterval = intensity;
    }

    private void generateFullFlow() {
        exitX = new ArrayList<Double>();
        exitX.add(Double.valueOf(0));
        double eps;
        int i = 1;
        while (exitX.get(exitX.size() - 1) <= modelTime) {
            eps = rand.nextDouble() * samplingInterval - (double) samplingInterval / 2;
            exitX.add(i * samplingInterval + eps);
            i = i + 1;
        }
        Double exitArray[] = new Double[exitX.size()];
        exitArray = exitX.toArray(exitArray);
        double exArray[] = new double[exitArray.length];
        for (int v = 0; v < exitArray.length; v++) {
            exArray[v] = exitArray[v];
        }
        innerVector = exArray;
    }

    public double getPeriod() {
        double period = 0;
        for (int i = 0; i < innerVector.length - 1; i++) {
            period = period + (innerVector[i + 1] - innerVector[i]);
        }
        period = period / (innerVector.length - 1);
        return period;
    }

    public double[] generate() {
        generateFullFlow();
        boolean omission;
        int n = 0;

        double[] tempY = new double[innerVector.length];
        for (int i = 0; i < innerVector.length; i++) {
            omission = rand.nextBoolean();
            if (omission == false) {
                n++;
                tempY[i] = innerVector[i];
            } else {
                tempY[i] = 65535;
            }
        }
        double[] readyX = new double[n];
        int j = 0;
        int i = 0;
        while (i < tempY.length) {
            if (tempY[i] != 65535) {
                readyX[j] = tempY[i];
                j++;
            }
            i++;
        }
        return readyX;
    }

}

  /* converted to utf8 */